//   封装  请求接口

//  导axios
import axios from 'axios'
//  配置基准路径
axios.defaults.baseURL = "http://klxin.cn:8888/api/private/v1"
//axios.defaults.baseURL = "http://127.0.0.1:8888/api/private/v1"
//axios.defaults.baseURL = "http://192.168.75.160:8888/api/private/v1"

import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  NProgress.start() //- 显示进度条
  // 在发送请求之前做些什么
  //console.log(config,44444);
  var token = window.sessionStorage.getItem("token")
  // console.log(token,445566);
  config.headers['Authorization'] = token
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  NProgress.done() //- 完成进度(进度条消失)
  // 对响应数据做点什么
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});


//   1-登录请求函数
export const login_api = (obj) => {
  return axios.post('/login', obj)
}

//  login_api({})

//  2-左侧菜单获取
export const menus_api = () => {
  return axios('/menus')
}


//  3-users数据获取
export const users_api = (params) => {
  return axios('/users', {
    params
  })
}

/*   menus_api({
     query:'',
     pagesize:1,
     pagenum:10
   }) */

//  
// axios('/users',{
//   params:{
//     query:'',
//     pagesize:1,
//     pagenum:10
//   }
// })


//  4-修改用户状态   users/:uId/state/:type====== users/2/state/true
// export const usersState_api = (params1,params2) => {
//   return axios.put(`users/${params1}/state/${params2}`)
// }
// //调用  usersState_api(2,true)

// users/:uId/state/:type======
export const usersState_api = (obj) => {
  return axios.put(`users/${obj.uId}/state/${obj.type}`)
}

// 调用  usersState_api({uId:2,type:true})

/* 4-删除单个用户   users/:id */
export const usersDel_api = (id) => {
  return axios.delete(`users/${id}`)
}
//  usersDel_api(3)

/* 5-编辑单个用户   users/:id */
export const usersEdit_api = (obj) => {
  return axios.put(`/users/${obj.id}`, {
    email: obj.email,
    mobile: obj.mobile
  })
}

//  usersDel_api({id:1,email:22@qq.com,mobile:133333})



// function aas(id) {
//   axios.put(`/users/${id}`)
// }

// aas(3)
// aas(2)


/* 6-添加单个用户   users/:id */
export const usersAdd_api = (obj) => {
  return axios.post(`/users`, {
    email: obj.email,
    mobile: obj.mobile,
    password: obj.password,
    username: obj.username
  })
}

//   usersAdd_api({ email:111@qq.com,mobile:11111111， password:666666,username:1333333})

/* 7-权限列表数据获取   rights/:type */
export const rights_api = (type) => {
  return axios.get(`/rights/${type}`)
}

//  rights_api('list')

/* 8-角色列表数据获取   rights/:type */
export const roles_api = () => {
  return axios.get(`/roles`)
}

/* 9-删除用户角色的权限   rights/:type */
export const rightsDel_api = (obj) => {
  return axios.delete(`roles/${obj.roleId}/rights/${obj.rightId}`)
}
//  rightsDel_api({roleId:1,rightId:2})

/* 10-给角色授权   roles/:roleId/rights*/
export const addRoleRights_api = (obj) => {
  return axios.post(`roles/${obj.roleId}/rights`, {
    rids: obj.rids
  })
}
//  addRoleRights_api({roleId:1,rids:'1,2,3'})

/* 11-分配用户角色  users/:id/role */
export const addUserRole_api = (obj) => {
  return axios.put(`users/${obj.id}/role`, {
    rid: obj.rid
  })
}
//  addRoleRights_api({id:1,rid:2})

/* 12-商品分类  categories */
export const cate_api = (obj) => {
  return axios.get(`categories`, {
    params: {
      type: obj.type,
      pagenum: obj.pagenum,
      pagesize: obj.pagesize
    }
  })
}

//  cate_api({ type:1,pagenum:2,pagesize:3})

//  获取  商品列表goods
//  13-users数据获取
export const goods_api = (params) => {
  return axios('/goods', {
    params
  })
}
//  goods_api({query：1，pagesize：5,pagenum:1})

//  15-获取参数列表   categories/:id/attributes
export const params_api = (obj) => {
  return axios(`categories/${obj.id}/attributes`, {
    params: {
      sel: obj.sel
    }
  })
}
//  params_api({id:1,sel:'only'})

//  14-添加动态参数或者静态属性  categories/:id/attributes/:attrId
export const addParams_api = (obj) => {
  return axios.put(`categories/${obj.id}/attributes/${obj.attrId}`, {
    attr_name: obj.attr_name,
    attr_sel: obj.attr_sel,
    attr_vals: obj.attr_vals
  })
}
//  addParams_api({id:1,attr_name: 2,attr_sel: only,attr_vals: '1,2,3'})

//   15- 添加参数或者属性
export const addAttributes_api = (obj) => {
  return axios.post(`categories/${obj.id}/attributes`, {
    attr_name: obj.attr_name,
    attr_sel: obj.attr_sel

  })
}
//    addAttributes_api({id:1, attr_name:1111,attr_sel:only})


//   16- 图片上传
export const upload_api = (obj) => {
  return axios.post(`upload`)
}


//   17- 添加商品
export const goodsAdd_api = (obj) => {
  return axios.post(`goods`, obj)
}  

//   18数据报表  reports/type/1
export const reports_api = (obj) => {
  return axios(`reports/type/1`)
}  